package feedback;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;

import com.util.HttpUtils;
import com.util.Info;
import com.zhny.wxginger.R;

public class FeedbackActivity extends Activity {

	private EditText mContactEdit = null;
	private EditText mNameEdit = null;
	private EditText mContentEdit = null;
	private ImageButton mLeftBtn = null;
	private ImageView mRightBtn = null;
	private Button mSubmitBtn = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.feedback);



		initView();
		InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.toggleSoftInput(InputMethodManager.RESULT_UNCHANGED_SHOWN,
				InputMethodManager.HIDE_NOT_ALWAYS);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if(keyCode==KeyEvent.KEYCODE_BACK&&event.getAction()==KeyEvent.ACTION_DOWN){
			FeedbackActivity.this.finish();
		}
		return super.onKeyDown(keyCode, event);
	}
	private void initView() {
    	mContactEdit = (EditText) findViewById(R.id.feedback_contact_edit);
    	mContentEdit = (EditText) findViewById(R.id.feedback_content_edit);
    	mNameEdit = (EditText) findViewById(R.id.feedback_name_edit);
    	mLeftBtn = (ImageButton) findViewById(R.id.btn_back);
    	mRightBtn = (ImageView) findViewById(R.id.right_btn);
    	mContentEdit.requestFocus();
    	
    	mLeftBtn.setVisibility(View.GONE);
    	mLeftBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				FeedbackActivity.this.finish();
			}
		});
    	mRightBtn.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				intent.setClass(FeedbackActivity.this, FeedbackRecordActivity.class);
				startActivity(intent);
			}
		});

    	mSubmitBtn = (Button) findViewById(R.id.submit_button);
    	mSubmitBtn.setOnClickListener(new OnClickListener() {
			
			@SuppressLint("ShowToast")
			public void onClick(View v) {
				String content = mContentEdit.getText().toString().trim();
				String contact = mContactEdit.getText().toString().trim();
				String name = mNameEdit.getText().toString().trim();
				if (content.equals("")) {
					Toast.makeText(FeedbackActivity.this, R.string.request_content,1).show();
					return;
				}else if(contact.equals("")){
					Toast.makeText(FeedbackActivity.this,"联系方式不能为空", 1).show();
				}else if(name.equals("")){
					Toast.makeText(FeedbackActivity.this, "姓名不能为空", 1).show();
				}
				System.out.println("lgq"+ Info.path2+content+"&lxfs="+contact+"&xm="+name);
				new SendFeedbackTask().execute(Info.path2+content+"&lxfs="+contact+"&xm="+name);
			}
		});
    }

	 class SendFeedbackTask extends AsyncTask<String, Void, String> {

		@SuppressLint("ShowToast")
		@Override
		protected void onPostExecute(String result) {
			if(result.equals("success")){
				Toast.makeText(FeedbackActivity.this, "反馈成功",1).show();
				mContactEdit.setText("");
				mContentEdit.setText("");
				mNameEdit.setText("");
				mContactEdit.setHint("请输入您的反馈意见");
				mContentEdit.setHint("联系方式:邮箱/QQ/手机号");
				mNameEdit.setHint("请输入您的名字");
			}else{
				Toast.makeText(FeedbackActivity.this, "反馈失败...",1).show();
			}
		}

		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub
			String result= HttpUtils.sendPostMethod(params[0], "utf-8");
			return result;
		}

	}
}
